<?php
class Main extends CI_Controller
{
	public $year = "";
	public $month = "";
	
	//main page function
	public function index($year=null,$month=null,$room=null)
	{
		//load model
		$this->load->model('Get_DB_Data_model');
		
		//set room session
		$room = $this->session->userdata('room');
		
		//get roomtype from room
		if($room != null)
		{
			$roomtype = $this->Get_DB_Data_model->get_roomtype_from_roomname($room);
			$this->session->set_userdata('roomtype', $roomtype);
		}
		
		//get calendar
		$data['calendar'] = $this->display($year, $month, $room);
		
		//get software data
		$data['soft_all'] = $this->Get_DB_Data_model->get_software_all();
		
		//get hardware data
		$data['hard_all'] = $this->Get_DB_Data_model->get_hardware_all();
		
		//get meeting room data
		$data['room_meeting'] = $this->Get_DB_Data_model->get_meeting_room_data();
		
		//get lab room data
		$data['room_lab'] = $this->Get_DB_Data_model->get_lab_room_data();
		
		$this->load->view('template/header');
		$this->load->view('main_view', $data);
		$this->load->view('template/footer');
	}
	//end main page function
	
	//display calendar function
	public function display($year, $month, $room)
	{
		if(!$year)
		{
			if($this->session->userdata('year')!=null){
				$year = $this->session->userdata('year');
			}
			else{
				$year = date('Y');
			}
		}
		if(!$month)
		{
			if($this->session->userdata('month')!=null){
				$month = $this->session->userdata('month');
			}
			else{
				$month = date('m');
			}
		}
		
		//set year and month session
		$this->session->set_userdata('year', $year);
		$this->session->set_userdata('month', $month);

		$this->load->model("Mycal_model");
		
		return $this->Mycal_model->generate($year, $month, $room);
	}
	//end display calendar function
	
	//change room ajax function
	public function ajax_change_room()
	{
		$room = $this->input->post('room');
		
		$this->session->set_userdata('room', $room);
		
		echo 'main/index';
	}
	//end change room ajax function
	
	
	//search room function
	public function search_room()
	{
		$this->load->model('Reservation_model');
		
		$roomtype = $this->input->post('roomtype');
		$peoplecount = $this->input->post('peoplecount');
		$date = $this->input->post('date');
		$hardware = $this->input->post('hardware');
		$time_start = $this->input->post('timestart');
		$time_end = $this->input->post('timeend');
		
		$sw = "";
		$hw = "";
		
		//get hardware select data
		for($i=0; $i<count($hardware); $i++){
			$hw = $hw ."%" .$hardware[$i];
		}
		
		//forward variable
		$data['hwuse'] = $hw;
		
		//set string like %
		$hw = $hw ."%";
		
		//forward variable to output page
		$data['date'] = $date;
		$data['time_start'] = $time_start;
		$data['time_end'] = $time_end;
		$data['peoplecount'] = $peoplecount;
		
		//Lab room
		if($roomtype == "labroom")
		{
			$comcount = $this->input->post('comcount');
			$software = $this->input->post('software');
			
			//get software select data
			for($i=0; $i<count($software); $i++){
				$sw = $sw ."%" .$software[$i];
			}
			
			//forward variable
			$data['swuse'] = $sw;
			$data['comcount'] = $comcount;
			
			//set string like %
			$sw = $sw ."%";
			
			//get search value
			$data['output'] = $this->Reservation_model->search_lab_room($peoplecount, $comcount, $sw, $hw, $date, $time_start, $time_end);
			
			//load value page
			$this->load->view('searchoutput_lab', $data);
		}
		
		//Meeting room
		if($roomtype == "meetingroom")
		{
			$data['output'] = $this->Reservation_model->search_meeting_room($peoplecount, $hw, $date, $time_start, $time_end);
			//load value page
			$this->load->view('searchoutput_meeting', $data);
		}
	}
	//end search room function
	
	//reserve lab room function
	public function reserve_lab()
	{
		//load model
		$this->load->model('Reservation_model');
		
		$memid = $this->session->userdata('memid');
		$roomname = $this->input->post('room_reserve');
		$usedate = $this->input->post('usedate');
		$usetime_start = $this->input->post('time_start');
		$usetime_end = $this->input->post('time_end');
		$resdate = date("Y-m-d");
		$restime = date('G:i:s');
		
		$software = $this->input->post('swuse');
		$swuse = substr($software, 1,strlen($software));
		$swuse = str_replace('%',',',$swuse);
		
		$hardware = $this->input->post('hwuse');
		$hwuse = substr($hardware, 1, strlen($hardware));
		$hwuse = str_replace('%', ',', $hwuse);
		
		$pcount = $this->input->post('pcount');
		$comcount = $this->input->post('comcount');
		
		$key = $this->generate_reservekey();
		
		//check reservation key
		$checkkey = $this->Reservation_model->check_duplicate_reservekey($key);
		while($checkkey == true)
		{
			$key = $this->generate_reservekey();
			$checkkey = $this->Reservation_model->check_duplicate_reservekey($key);
			
		}
		
		$reserve_into_db = $this->Reservation_model->reserve_lab_room($memid, $roomname, $usedate, $usetime_start, $usetime_end, $resdate, $restime, $swuse, $hwuse, $pcount, $comcount, $key);
		
		if($reserve_into_db == true)
		{
			//load email model
			$this->load->model('Email_model');
			
			//config message
			$subject = "รายงานผลการจองห้อง";
			$message = "คุณได้ทำการจองห้องเรียบร้อยแล้ว<br/> คุณสามารถตรวจสอบสถานะการจองได้ที่" .base_url() ."reservation/status/" .$key;
			$send_to = $this->session->userdata('email');
			
			//send email
			$this->Email_model->send_email($send_to, $subject, $message);
			redirect("reservation/status/" .$key);
		}
		
	}
	//end reserve lab room function
	
	//reserve meeting room function
	public function reserve_meeting()
	{
		//load model
		$this->load->model('Reservation_model');
		
		$memid = $this->session->userdata('memid');
		$roomname = $this->input->post('room_reserve');
		$usedate = $this->input->post('usedate');
		$usetime_start = $this->input->post('time_start');
		$usetime_end = $this->input->post('time_end');
		$resdate = date("Y-m-d");
		$restime = date('G:i:s');
		
		$hardware = $this->input->post('hwuse');
		$hwuse = substr($hardware, 1, strlen($hardware));
		$hwuse = str_replace('%', ',', $hwuse);
		
		$pcount = $this->input->post('pcount');
		$comcount = $this->input->post('comcount');
		
		$key = $this->generate_reservekey();
		
		//check reservation key
		$checkkey = $this->Reservation_model->check_duplicate_reservekey($key);
		while($checkkey == true)
		{
			$key = $this->generate_reservekey();
			$checkkey = $this->Reservation_model->check_duplicate_reservekey($key);
			
		}
		
		$reserve_into_db = $this->Reservation_model->reserve_meeting_room($memid, $roomname, $usedate, $usetime_start, $usetime_end, $resdate, $restime, $hwuse, $pcount, $key);
		
		if($reserve_into_db == true)
		{
			//load email model
			$this->load->model('Email_model');
			
			//config message
			$subject = "รายงานผลการจองห้อง";
			$message = "คุณได้ทำการจองห้องเรียบร้อยแล้ว<br/> คุณสามารถตรวจสอบสถานะการจองได้ที่" .base_url() ."reservation/status/" .$key;
			$send_to = $this->session->userdata('email');
			
			//send email
			$this->Email_model->send_email($send_to, $subject, $message);
			redirect("reservation/status/" .$key);
		}
		
	}
	//end reserve meeting room function
	
	//generate reserve key 
	public function generate_reservekey()
	{
		$character = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9');
		
		//random key from array
		shuffle($character);
		
		$key = "";
		
		for($i=0; $i<8; $i++)
		{
			$key = $key .$character[$i];
		}
		
		return $key;
		
	}
	//end generate reserve key
	
	
}